///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_IMAGE_DRAWING_BASIC_H_
#define _BUOLA_IMAGE_DRAWING_BASIC_H_

#include <buola/image/format.h>

namespace buola { namespace img {

//! \name drawing
//@{

template <typename tView>
inline void draw_cross(const tView& pDst, const typename tView::TPixel& pPixel,
                  int pX,int pY,int pSize=10) 
{
    int lYFrom=std::max(0,pY-pSize);
    int lYTo=std::min((int)pDst.Height()-1,pY+pSize);
    int lXFrom=std::max(0,pX-pSize);
    int lXTo=std::min((int)pDst.Width()-1,pX+pSize);

    auto lYIt=pDst.ColBegin(pX);
    
    for(int y=lYFrom;y<=lYTo;y++)
    {
        lYIt[y]=pPixel;
    }

    auto lXIt = pDst.RowBegin(pY);
    
    for(int x=lXFrom;x<=lXTo;x++)
    {
        lXIt[x]=pPixel;
    }
}

template<typename tView>
inline void draw_frame(const tView &pDst,const typename tView::TPixel &pPixel)
{
    for(auto i=pDst.Row(0).begin();i!=pDst.Row(0).end();++i)
        *i=pPixel;
    for(auto i=pDst.Row(pDst.Height()-1).begin();i!=pDst.Row(pDst.Height()-1).end();++i)
        *i=pPixel;
    for(auto i=pDst.Col(0).begin();i!=pDst.Col(0).end();++i)
        *i=pPixel;
    for(auto i=pDst.Col(pDst.Width()-1).begin();i!=pDst.Col(pDst.Width()-1).end();++i)
        *i=pPixel;
}

template<typename tView>
inline void fill_hline(const tView &pDst,int x,int y,int pLen,const typename tView::TPixel &pPixel)
{
    auto lIt=pDst.XIterator(x,y);
    while(pLen--)
    {
        *lIt=pPixel;
        ++lIt;
    }
}

template<typename tView>
inline void fill_vline(const tView &pDst,int x,int y,int pLen,const typename tView::TPixel &pPixel)
{
    auto lIt=pDst.YIterator(x,y);
    while(pLen--)
    {
        *lIt=pPixel;
        ++lIt;
    }
}

template<typename tView>
inline void blend_hline(const tView &pDst,int x,int y,int pLen,const typename tView::TPixel &pPixel,uint8_t pCover)
{
    if(pCover==255) return fill_hline(pDst,x,y,pLen,pPixel);
    auto lIt=pDst.XIterator(x,y);
    while(pLen--)
    {
        (*lIt).Blend(pPixel,pCover);
        ++lIt;
    }
}

template<typename tView>
inline void blend_vline(const tView &pDst,int x,int y,int pLen,const typename tView::TPixel &pPixel,uint8_t pCover)
{
    if(pCover==255) return fill_vline(pDst,x,y,pLen,pPixel);
    auto lIt=pDst.YIterator(x,y);
    while(pLen--)
    {
        (*lIt).Blend(pPixel,pCover);
        ++lIt;
    }
}

template<typename tView>
inline void fill_hspan(const tView &pDst,int x,int y,int pLen,const typename tView::TPixel *pSpan)
{
    auto lIt=pDst.XIterator(x,y);
    while(pLen--)
    {
        *lIt=*(pSpan++);
        ++lIt;
    }
}

template<typename tView>
inline void blend_hspan(const tView &pDst,int x,int y,int pLen,const typename tView::TPixel &pPixel,const uint8_t *pCovers)
{
    auto lIt=pDst.XIterator(x,y);
    while(pLen--)
    {
        (*lIt).Blend(pPixel,*pCovers);
        ++lIt;
        ++pCovers;
    }
}

template<typename tView>
inline void fill_rect(const tView &pDst,int x,int y,int w,int h,const typename tView::TPixel &pPixel)
{
    while(h--)
    {
        fill_hline(pDst,x,y,w,pPixel);
        y++;
    }
}

template<typename tView>
inline void blend_rect(const tView &pDst,int x,int y,int w,int h,const typename tView::TPixel &pPixel,uint8_t pCover)
{
    while(h--)
    {
        blend_hline(pDst,x,y,w,pPixel,pCover);
        y++;
    }
}

//@}
    
/*namespace image*/ } /*namespace buola*/ }

#endif
